package com.ansion.commonchat.contact.systemcontact;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.ansion.commonchat.BaseApplication;
import com.ansion.constant.BConstant;

public class SystemContactUtils {

	/**
	 * 若 activity 不为null ，则要走带有返回值的打开方式
	 */

	public static void startLocalFriendAddActivity(String phone , String name, Activity activity){
		Intent intent = new Intent(Intent.ACTION_INSERT);
//		intent.setType("vnd.android.cursor.dir/person");
//		intent.setType("vnd.android.cursor.dir/contact");
		intent.setType("vnd.android.cursor.dir/raw_contact");
		// 联系人姓名
		if(!TextUtils.isEmpty(name)){
			intent.putExtra(ContactsContract.Intents.Insert.NAME, name );
		}
		if(!TextUtils.isEmpty(phone)){
			intent.putExtra(ContactsContract.Intents.Insert.PHONE , phone);
		}
		try {
			if (activity != null){
				activity.startActivityForResult(intent, BConstant.Contact.REQUEST_CODE_EDIT_CONTACT);
			}else {
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				BaseApplication.getContext().startActivity(intent);
			}
		} catch (Exception e) {
		}
	}


	/**
	 * 若 activity 不为null ，则要走带有返回值的打开方式
	 */
	public static void addToExistContact(int contactId , String phone, Activity activity){

		try{
			Intent intent = new Intent(Intent.ACTION_EDIT, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId)));
			intent.putExtra(ContactsContract.Intents.Insert.PHONE , phone);
			if (activity != null){
				activity.startActivityForResult(intent, BConstant.Contact.REQUEST_CODE_EDIT_CONTACT);
			}
			else {
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				BaseApplication.getContext().startActivity(intent);
			}
		}catch(Exception e){

		}

	}

}
